﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PersonaLibrary.Data;
using PersonaLibrary.Validation;

namespace PersonaLibrary.ViewModel.EmployeeNoteLinkViewModel
{
    internal class EmployeeNoteLinkVM : LinqEditableItem<EmployeeNoteLink>
    {
        private readonly ValidationItem _empValid = new ValidationItem();
        private readonly ValidationItem _notValid = new ValidationItem();

        public EmployeeNoteLinkVM()
        {
            EditableItem = new EmployeeNoteLink();
            InitValid();
        }

        public EmployeeNoteLinkVM(int id)
        {
            EditableItem = Context.EmployeeNoteLinks.Single(x => ((EmployeeNoteLink)x).Id == id);
            InitValid();
            _empValid.ValidationValue.Key = EditableItem.Employee.ToString();
            _empValid.IsValid = true;
            _notValid.ValidationValue.Key = EditableItem.Note.Name;
            _notValid.IsValid = true;
        }

        private void InitValid()
        {
			_empValid.PropertyChanged += EmpValidPropertyChanged;
        }

        void EmpValidPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ValidationValue")
            {
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\s+");
                string[] result = reg.Replace(_empValid.ValidationValue.Key, " ").Split(" ".ToCharArray());
                switch (result.Length)
                {
                }
            }
        }

        public ValidationItem EmpValid
        {
            get
            {
                return _empValid;
            }
        }

        public ValidationItem NotValid
        {
            get
            {
                return _notValid;
            }
        }

        public override bool CanCommit()
        {
            if (!_notValid.IsValid) return false;
            if (!_empValid.IsValid) return false;
            return true;
        }
    }
}
